IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cms_sp_PageAddPageControl]') AND type in (N'P', N'PC'))
	DROP PROCEDURE [dbo].[cms_sp_PageAddPageControl]
GO
create procedure [dbo].[cms_sp_PageAddPageControl]
(
	@Id int = null output,
	@SiteGroupId int = null,
	@SiteId int = null,
	@SystemLanguageId int = null,
	@WorkflowId uniqueidentifier = null,
	@IsLatestCompletedRevision bit = null,
	@IsPublished bit = null,
	@PageId int,
	@TemplateRegionId int,
	@ModuleId int = null,
	@PageControlTypeKey int,
	@ContentId int = null,
	@PageControlContent nvarchar(max) = null,
	@PageControlModelValue xml = null,
	@PageControlViewValue xml = null,
	@CreatedBy int,
	@DateCreated datetime,
	@ContentTypeParameter nvarchar(400),
	@PageContentTypeParameter nvarchar(150),
	@PageControlTypeParameter nvarchar(150),
	@PageControlOrder int,
	@AdminCaption nvarchar(255) = null
)
as
declare @IsLatestRevision bit
declare @IsDeleted bit
if @IsPublished is null
begin
	set @IsPublished = 1
end
set @IsDeleted = 0
set @IsLatestRevision = 1 
if @IsLatestCompletedRevision is null
begin
	set @IsLatestCompletedRevision = @IsLatestRevision
end
if @PageControlTypeKey = 1 -- page content
begin
	declare @ContentTypeId int
	select 
		@ContentTypeId = ContentTypeId 
	from [dbo].[ContentTypes]
	where ContentTypeName = @PageContentTypeParameter
	insert into dbo.Contents
	(
		SiteId,
		SiteGroupId,
		SystemLanguageId,
		ContentTypeId,
		PublicContentId,
		ContentTitle,
		ContentBody,
		CreatedBy,
		DateCreated
	)
	values
	(
		@SiteId,
		@SiteGroupId,
		@SystemLanguageId,
		@ContentTypeId,
		0,
		'page-content',
		@PageControlContent,
		@CreatedBy,
		@DateCreated
	)
	set @ContentId = SCOPE_IDENTITY()
	update dbo.Contents
		set PublicContentId = @ContentId
	where ContentId = @ContentId
	insert into dbo.EntityRevisions
	(
		EntityId,
		PublicEntityId,
		EntityTypeParameter,
		WorkflowId,
		IsLatestRevision,
		IsLatestCompletedRevision,
		IsDeleted,
		IsPublished
	)
	values
	(
		@ContentId,
		@ContentId,
		@ContentTypeParameter,
		@WorkflowId,
		@IsLatestRevision,
		@IsLatestCompletedRevision,
		@IsDeleted,
		@IsPublished
	)
end
insert into dbo.PageControls
(
	PageId,
	TemplateRegionId,
	ModuleId,
	PageControlContentId,
	PageControlTypeKey,
	PageControlViewValue,
	PageControlModelValue,
	PageControlOrder,
	CreatedBy,
	DateCreated,
	AdminCaption
)
values
(
	@PageId,
	@TemplateRegionId,
	@ModuleId,
	@ContentId,
	@PageControlTypeKey,
	@PageControlViewValue,
	@PageControlModelValue,
	@PageControlOrder,
	@CreatedBy,
	@DateCreated,
	@AdminCaption
)
set @Id = SCOPE_IDENTITY()
insert into dbo.EntityRevisions
(
	EntityId,
	PublicEntityId,
	EntityTypeParameter,
	WorkflowId,
	IsLatestRevision,
	IsLatestCompletedRevision,
	IsDeleted,
	IsPublished
)
values
(
	@Id,
	@Id,
	@PageControlTypeParameter,
	@WorkflowId,
	@IsLatestRevision,
	@IsLatestCompletedRevision,
	@IsDeleted,
	@IsPublished
)
return @Id


GO

